package com.nervenets.template.hibernate.domain.user;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.nervenets.general.hibernate.DomainObject;
import com.nervenets.general.utils.AESUtils;
import com.nervenets.template.enumeration.Gender;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;
import lombok.experimental.SuperBuilder;
import org.apache.commons.lang3.StringUtils;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

@ApiModel("用户表")
@Entity
@Table
@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor
@SuperBuilder
public class User extends DomainObject {
    @ApiModelProperty(value = "账号")
    @org.hibernate.annotations.Index(name = "_idx_user_mobile")
    @Column(columnDefinition = "varchar(20)")
    private String mobile;
    @ApiModelProperty(value = "密码")
    @JsonIgnore
    @JSONField(serialize = false)
    private String password;
    @ApiModelProperty(value = "昵称")
    @Column(columnDefinition = "varchar(20)")
    private String name;
    @ApiModelProperty(value = "头像")
    private String avatar;
    @ApiModelProperty(value = "微信unionId")
    @Column(columnDefinition = "varchar(64)")
    private String unionId;
    @ApiModelProperty(value = "性别")
    @Column(columnDefinition = "tinyint")
    @Builder.Default
    private Gender gender = Gender.female;
    @ApiModelProperty(value = "是否启用")
    @Builder.Default
    private boolean enable = true;
    @ApiModelProperty(value = "生日")
    private long birthday;
    @ApiModelProperty(value = "最后登录时间")
    private long lastLoginTime;
    @Column(columnDefinition = "varchar(20)")
    @ApiModelProperty(value = "省份")
    private String province;
    @Column(columnDefinition = "varchar(20)")
    @ApiModelProperty(value = "城市")
    private String city;
    @ApiModelProperty(value = "经度")
    private double longitude = 0;
    @ApiModelProperty(value = "纬度")
    private double latitude = 0;
    @ApiModelProperty(value = "邀请人ID")
    private long inviteUserId;
    @ApiModelProperty(value = "是否关注")
    private boolean subscribe;
    @ApiModelProperty(value = "附加内容")
    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
    @OrderBy(value = "createTime")
    @JoinColumn(name = "user_id")
    private List<UserExtra> extras = new ArrayList<>();
    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
    @OrderBy(value = "createTime")
    @JoinColumn(name = "user_id")
    private List<UserWxId> wxIds = new ArrayList<>();
    @Transient
    private JSONObject surplus;

    public User(String mobile, String password, String name, String avatar, String unionId, Gender gender, boolean enable) {
        this.mobile = mobile;
        this.password = password;
        this.name = name;
        this.avatar = avatar;
        this.unionId = unionId;
        this.gender = gender;
        this.enable = enable;
    }

    public String decryptPassword() {
        return StringUtils.trim(AESUtils.decryptString(this.password));
    }

    public void generateNewPassword(String password) {
        this.password = AESUtils.encryptString(password);
    }
}
